package com.erik.ipmonitor.controller;

import com.erik.ipmonitor.model.vo.SearchIndexVO;
import com.erik.ipmonitor.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * SearchController
 *
 * @author : erik.zhou
 * @date : 2025/08/25/15:42
 * @description :
 */
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private ISearchService searchService;

    @RequestMapping("/page")
    public String index() {
        return "searchPage";
    }

    @GetMapping("/doSearch")
    @ResponseBody
    public Page<SearchIndexVO> search(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "50") int size,
            @RequestParam(name = "keyword") String keyword) {
        return searchService.doSearch(page, size, keyword);
    }


    @PostMapping("/loadFile")
    @ResponseBody
    public void loadFile(
            @RequestParam(name = "file") MultipartFile file,
            @RequestParam(name = "encoding") String encoding
    ) {
        searchService.loadFile(file, encoding);
    }
}
